<template>
    <view>
        <!-- d:\applet\byteApplet\dy-wallpaper-applet-2\pages\talent\talent.ttml -->

        <view class="tab-content" style="padding-top: 10px">
            <!--达人列表 begin-->
            <view class="jiangqie-news-icon">
                <view v-for="(item,index) in list" :key="index" :data-userid="item.userid" class="jiangqie-news-iconbox" @click.stop.prevent="handlerRaRenClick">
                    <view class="jiangqie-news-iconbg">
                        <image class="jiangqie-news-iconimg" :src="item.avatarUrl == null ? '../../static/images/user.png' : item.avatarUrl"></image>
                    </view>
                    <text>{{ item.word }}</text>
                    <text style="color: #c4c4c4">作品数量:{{ item.worksCount }}</text>
                </view>
               
            </view>

            <!--达人列表 end-->
        </view>

        <!--加载框 start-->
        <tui-loadmore :visible="loadding"></tui-loadmore>
        <tui-nomore :visible="!pullUpOnLast" bgcolor="#fbfbfb"></tui-nomore>
        <!--加载框 end-->
    </view>
</template>

<script>
import tuiLoading from '../../components/loading/loading';
import tuiLoadmore from '../../components/loadmore/loadmore';
import tuiNomore from '../../components/nomore/nomore';
const Constants = require('../../utils/constants');

const Rest = require('../../utils/rest');

const http = require('../../utils/http.js');

const util = require('../../utils/util.js');

let setinad;
export default {
    components: {
        tuiLoading,
        tuiLoadmore,
        tuiNomore
    },
    data() {
        return {
            list: [],
            pullUpOnLast: true,
            loadding: false
        };
    },
    onLoad: function (options) {
		console.log(options);
        this.loadTalents(true);
    },
    onReachBottom: function () {
		
        if (!this.pullUpOnLast) {
            return;
        }

        this.loadTalents(false);
    },
	onShow:function(){
		var that = this; //分享
		util.getAD(
		    that,
		    function () {
		        that.setInterstitialAd(); //加载插屏广告
		    });
	},
	onHide() {
	    clearInterval(setinad);
	},
    methods: {
		// 获取小程序插屏广告
		setInterstitialAd: function () {
		    var that = this;
		
		    if (that.setAD.interstitialid && uni.createInterstitialAd) {
		        let interstitialAd = uni.createInterstitialAd({
		            adUnitId: that.setAD.interstitialid
		        }); // 监听插屏错误事件
		
		        interstitialAd.onError((err) => {
		            console.error(err);
		        }); // 显示广告
		
		        if (interstitialAd) {
		            if (that.setAD.switch_inad == 'yes') {
		                setinad = setInterval(() => {
		                    interstitialAd.show().catch((err) => {
		                        console.error(err);
		                    });
		                }, 2000);
		            } else {
		                setTimeout(() => {
		                    interstitialAd.show().catch((err) => {
		                        console.error(err);
		                    });
		                }, 6000);
		            }
		        }
		    }
		},
        loadTalents: function (loadRefresh) {
            let that = this;
            that.setData({
                loadding: true
            });
            let offset = 0;

            if (!loadRefresh) {
                offset = that.list.length;
            }

            var params = {
                url: '/talent/list',
                method: 'GET',
                data: {
                    offset: offset
                },
                callBack: function (res) {
                    if (res.code == 200) {
                        let lista = res.data.list;

                        // for (let i = 0; i < lista.length; i++) {
                        //     if (lista[i].nickName != '' && lista[i].nickName != null) {
                        //         if (lista[i].nickName.length > 8) {
                        //             let name = lista[i].nickName.length > 8 ? lista[i].nickName.substring(0, 8) : lista[i].nickName;
                        //             lista[i].nickName = name;
                        //         }
                        //     }
                        // }

                        that.setData({
                            loadding: false,
                            list: loadRefresh ? lista : that.list.concat(lista),
                            pullUpOnLast: !res.data.isLastPage ? true : false
                        });
                    } else {
                        uni.showToast({
                            title: res.msg,
                            icon: 'fail',
                            duration: 2000
                        });
                    }
                }
            };
            http.request(params);
        },

        handlerRaRenClick: function (e) {
            let userid = e.currentTarget.dataset.userid;
            userid = userid.replace(/\s+/g, ''); //全局替换空格

            uni.navigateTo({
                url: '/pages/talent/mytalent/mytalent?userid=' + userid
            });
        }
    }
};
</script>
<style>
@import './talent.css';
</style>
